package com.teamc.team;

import java.util.Scanner;

/**
 * @author ding
 * @since 2020-6-28
 */
public class demo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("———————————————————————————————————————————");
        System.out.println("欢迎进入 <<只要算不死|就往死里算>>  进制转换器！");
        System.out.println("             ————支持小数————               ");
        System.out.println("             最高支持三十六进制            ");
        System.out.println("————————————————请输入元数据————————————————");
        String ff = scanner.next();
        System.out.println("—————————————请输入元数据进制———————————————");
        int i = scanner.nextInt();
        System.out.println("——————————————请输入目标进制————————————————");
        int target = scanner.nextInt();
        if (ff.contains(".")) {
            double v = BtoX((ff.split("\\.")[0]), i, false) + BtoX((ff.split("\\.")[1]), i, true);
            int length = Math.min((v + "").split("\\.")[1].length(), 8);
            String s = XtoA((v + "").split("\\.")[0], target, false).equals("")?"0":XtoA((v + "").split("\\.")[0],target,false);
            System.out.println("您要的结果是"+s+"." +XtoA((v + "").split("\\.")[1].substring(0,length),target,true));
        } else {
            System.out.println("您要的结果是"+XtoA((BtoX(ff, i, false) + "").split("\\.")[0],target,false));
        }
    }

    private static String XtoA(String value,int a,boolean isFractionalPart) {
        StringBuilder result = new StringBuilder();
        if (isFractionalPart) {
            double s = Double.parseDouble("0."+value),j=20;
            while (s != (int)s && j != 0) {
                s *= a;
                int i = (int) s;
                if (i>=10)
                    result.append((char)(i+'A'-10));
                else
                    result.append(i);
                s = s-(int)s;
                j--;
            }
            return result.toString();
        }
        int i1 = Integer.parseInt(value);
        while (i1 != 0) {
            int i = i1 % a;
            if (i>=10)
                result.append((char) (i+'A'-10));
            else
                result.append(i);
            i1 /= a;
        }
        return result.reverse().toString();
    }

    private static double BtoX( String s,int b,boolean isFractionalPart) {
        int sum = 0,length = s.length()-1;
        for (char c : s.toCharArray()) {
            if (c >= 'A' && c <= 'Z')
                sum += (c - 'A' + 10) * Math.pow(b,length--);
            else
                sum += Character.getNumericValue(c) * Math.pow(b,length--);
        }
        if (isFractionalPart)
            return sum/(Math.pow(b,s.length()));
        return sum;
    }
}